<template>
  <div >
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">考试管理</el-breadcrumb-item>
      <el-breadcrumb-item>考试列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <div style="margin: 0 auto">
      <el-form :label-position="left" :inline="true" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="考试名称">
          <template>
            <el-select v-model="ruleForm.id" placeholder="请选择">
              <el-option
                  v-for="item in options"
                  :label="item.title"
                  :value="item.id">
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="考生姓名">
          <el-input placeholder="请输入内容" v-model="ruleForm.applyName" >
          </el-input>
        </el-form-item>
        <el-form-item label="身份证号">
          <el-input placeholder="请输入内容" v-model="ruleForm.idNumber" >
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button plain type="primary" @click="submitForm('ruleForm')">查询</el-button>
        </el-form-item>
      </el-form>

      <el-divider></el-divider>
      <div style="width: 500px;margin: 0 auto">
        <h1 style="text-align: center">成绩列表</h1>
        <div>

          <h2>考生姓名：{{tableData.applyName}}</h2>
          <el-divider></el-divider>
          <h2>考试项目：{{tableData.title}}</h2>
          <el-divider></el-divider>
          <h2>考试分数：{{tableData.score}}</h2>
          <el-divider></el-divider>
        </div>

      </div>
    </div>



  </div>
</template>
<style>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 100px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  padding-left: 110px;
  margin-right: 10px;
  margin-bottom: 0;
  width: 50%;
}
</style>
<script>
export default {
  data() {
    return {
      options:'',
      ruleForm:{
        id:'',
        applyName:'',
        idNumber:'110101202304281239'
      },
      tableData: {
        applyName:'',
        title:'',
        score:''
      }
      ,
      rules: {
        /*trigger: 'blur'触发方式,丢失焦点*/
        applyName: [
          { required: true, message: '请输入考生姓名', trigger: 'blur' },
          {  min: 2, max: 15, message: '长度在 3 到 5 个字符', trigger: 'blur'  }
        ],
        idNumber: [
          { required: true, message: '请输入身份证号', trigger: 'blur' },
          {   pattern:/^[1-9]\d{5}(19\d{2}|20[0-2]\d)(0[1-9]|1[0-2])(0[1-9]|[1-2]\d|3[0-1])\d{3}[\dxX]$/ , message: '请输入正确的身份证号', trigger: 'blur'  }
        ],
      }
    }
  },
  methods: {
    submitForm() {
      let url = "http://120.77.154.166:8080/exam/checkResult";
      let formData = this.qs.stringify(this.ruleForm);
      console.log(formData)
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url,formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          console.log(jsonResult.data)
          this.tableData.applyName = jsonResult.data.applyName;
          this.tableData.title = jsonResult.data.title;
          this.tableData.score = jsonResult.data.score;
        }else if (jsonResult.state == 40900) {
          this.$alert(jsonResult.message, '查询失败', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
          this.resetForm(formName);
        } else {
          this.$message({
            message: '查询失败！',
            type: 'success'
          });
          this.resetForm(formName);
        }
      });
    },

    loadExam() {
      let url = "http://120.77.154.166:8080/exam/list";
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          console.log(jsonResult.data)
          this.options = jsonResult.data;
        }
      });
    }
  },
  mounted() {
    this.loadExam();
  }
}
</script>
